package com.example.user.userUtil;

import com.example.user.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

/**
 * @author 贾明祯
 * @ClassName UserMessage
 * @Date 2020/8/17 11:53
 * @Introduce 当前登录用户信息
 */
@Component
public class UserMessage {
    /**
     * @Author: 贾明祯
     * @Date: 2020/8/17 11:58
     * @Introduce: 设置session
     */
    public static void setUser(HttpSession session,User user){
        session.setAttribute("userMessage",user);
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/17 11:58
     * @Introduce: 根据当前session，获取用户id
     */
    public static String userId(HttpSession session){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        User user = (User) session.getAttribute("userMessage");
        if(user!=null){
            return user.getId();
        }
        return null;
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/17 11:58
     * @Introduce: 根据当前session，获取用户手机号
     */
    public static String userPhone(HttpSession session){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        User user = (User) session.getAttribute("userMessage");
        if(user!=null){
            return user.getPhone();
        }
        return null;
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/17 11:58
     * @Introduce: 根据当前session，获取用户邮箱
     */
    public static String userEmail(HttpSession session){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        User user = (User) session.getAttribute("userMessage");
        if(user!=null){
            return user.getEmail();
        }
        return null;
    }
    
    /**
      * @Author: 贾明祯
      * @Date: 2020/8/17 13:19
      * @Introduce: 根据当前session，获取用户所有信息
      */
    public static User userMessage(HttpSession session){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        User user = (User) session.getAttribute("userMessage");
        if(user!=null){
            return user;
        }
        return null;
    }
}
